from django.contrib.auth.models import User
from django.db import models
from user.models import Address
from utils.modelsMixin import ModelSetMixin, DateTimeModelMixin


# 分类表
class Classification(ModelSetMixin):
    name = models.CharField(max_length=40, verbose_name='分类名')
    parent = models.ForeignKey('self', null=True, blank=True, on_delete=models.SET_NULL, verbose_name='上级分类')

    class Meta:
        db_table = 'classification'
        verbose_name = '商品分类'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 商品表
class Commodity(ModelSetMixin):
    STATUS_CHOICE = (
        (0, '未发布'),
        (1, '已发布')
    )
    name = models.CharField(max_length=40, verbose_name='商品名')
    caption = models.CharField(max_length=140, verbose_name='副标题')
    brand = models.CharField(max_length=40, verbose_name='品牌', null=True, blank=True)
    # DecimalField(max_digits=最大数位，decimal_places=精确到小数位后的位数)
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='单价')
    stock = models.IntegerField(verbose_name='库存')
    pack = models.TextField(verbose_name='包装信息', null=True, blank=True)
    service_after_sale = models.TextField(verbose_name='售后服务', null=True, blank=True)
    sales = models.IntegerField(verbose_name='销量', default=0)
    comments = models.IntegerField(verbose_name='评论数', default=0)
    status = models.IntegerField(verbose_name='状态', default=0, choices=STATUS_CHOICE)
    detail = models.TextField(verbose_name='详情', null=True, blank=True)

    # 因为一级和二级分类都是关联Classification模型，因此反向查询时，管理器用related_name，而不是原模型小写_set(commodity_set)
    classification1 = models.ForeignKey(Classification, on_delete=models.PROTECT, verbose_name='一级分类',
                                        related_name='classification1')
    classification2 = models.ForeignKey(Classification, on_delete=models.PROTECT, verbose_name='二级分类',
                                        related_name='classification2', null=True, blank=True)

    class Meta:
        ordering = ['-sales', '-comments', '-c_time']
        db_table = 'commodity'
        verbose_name = '商品'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.id}-{self.brand}-{self.name}'


# 商品图片表
class CommodityImg(models.Model):
    src = models.TextField(verbose_name='图片地址')
    priority = models.IntegerField(verbose_name='优先级', default=0)
    commodity = models.ForeignKey(Commodity, on_delete=models.CASCADE, verbose_name='商品')

    class Meta:
        ordering = ['priority']
        db_table = 'commodity_img'
        verbose_name = '商品图'
        verbose_name_plural = verbose_name


# 购物车
class ShoppingCart(DateTimeModelMixin):
    number = models.IntegerField(default=1, verbose_name='数量')
    commodity = models.ForeignKey(Commodity, on_delete=models.CASCADE, verbose_name='商品')
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')

    class Meta:
        ordering = ['-c_time']
        db_table = 'shopping_cart'
        verbose_name = '购物车'
        verbose_name_plural = verbose_name


# 订单
class Order(DateTimeModelMixin):
    STATUS_CHIOCE = (
        (1, '待支付'),
        (2, '待发货'),
        (3, '待收货'),
        (4, '待评价'),
        (5, '已完成'),
        (6, '已取消'),
    )
    PAY_METHOD_CHIOCE = (
        (1, '支付宝'),
        (2, '货到付款'),
    )

    order_id = models.CharField(max_length=64, primary_key=True, verbose_name='订单编号')
    total_count = models.IntegerField(default=1, verbose_name='商品总数')
    total_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='订单总金额')
    status = models.IntegerField(default=1, choices=STATUS_CHIOCE, verbose_name='订单状态')
    pay_method = models.IntegerField(default=1, choices=PAY_METHOD_CHIOCE, verbose_name='支付方式')

    address = models.ForeignKey(Address, on_delete=models.PROTECT, verbose_name='收货地址')
    user = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name='用户')

    class Meta:
        ordering = ['-c_time']
        db_table = 'order'
        verbose_name = '订单'
        verbose_name_plural = verbose_name


# 订单商品
class OrderGoods(DateTimeModelMixin):
    SCORE_CHIOCE = (
        (0, '零星'),
        (1, '一星'),
        (2, '二星'),
        (3, '三星'),
        (4, '四星'),
        (5, '五星'),
    )
    number = models.IntegerField(default=1, verbose_name='数量')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='单价')
    comment = models.TextField(verbose_name='评论')
    score = models.IntegerField(verbose_name='评分', null=True, blank=True, choices=SCORE_CHIOCE)
    is_anonymous = models.BooleanField(default=False, verbose_name='匿名评价')

    order = models.ForeignKey(Order, on_delete=models.CASCADE, verbose_name='订单')
    commodity = models.ForeignKey(Commodity, on_delete=models.PROTECT, verbose_name='商品')

    class Meta:
        db_table = 'order_goods'
        verbose_name = '订单商品'
        verbose_name_plural = verbose_name


# 支付信息
class Payment(DateTimeModelMixin):
    trade_id = models.CharField('交易支付号', max_length=100, unique=True)
    order = models.ForeignKey(Order, on_delete=models.CASCADE, verbose_name='订单')

    class Meta:
        ordering = ['-c_time']
        db_table = 'payment'
        verbose_name = '支付信息'
        verbose_name_plural = verbose_name
